package com.foreveross.crawl.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.filters.StringInputStream;

import com.google.common.collect.Lists;

/**
 * 正则表达式方法用于解析和获取HTML内容
 * 
 * @author spring.华 e-mail 13719279477@139.com 2012-11-16
 */
public class RegHtmlUtil {

	public static final Log LOG = LogFactory.getLog(RegHtmlUtil.class);

	/**
	 * 用正则获取网上的信息
	 * 
	 * @param str
	 * @param mode "<span>\\s*\\n*
	 *            <hr>
	 *            \\s*\\n*\\t*<br>
	 *            ([^/]+)<br>
	 *            \\s*\\n*\\t*</div>"
	 * @return
	 */
	public static String regStr(String sHtml, String mode) {
		Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
		Matcher m = p.matcher(sHtml);
		String reg_s = null;
		if (m.find()) {
			reg_s = m.group(1);
		}
		return reg_s;
	}

	/**
	 * @param sHtml
	 * @param mode 正则
	 * @param i 匹配第几个组
	 * @return
	 */
	public static ArrayList<String> retrieveLinks(String sHtml, String mode, int i) {
		Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
		try {
			if ((sHtml == null) || (sHtml == "")) return null;
			Matcher m = p.matcher(sHtml);
			ArrayList<String> linkList = new ArrayList<String>();
			while (m.find()) {
				String link = m.group(i).trim();
				linkList.add(link);
			}
			return linkList;
		} catch (RuntimeException e) {
			LOG.info("正则匹配出现异常!");
			e.printStackTrace();
		}
		return null;
	}

	public static ArrayList<List<String>> retrieveLinkss(String sHtml, String mode) {
		Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL + Pattern.MULTILINE);
		try {
			if ((sHtml == null) || (sHtml == "")) return null;
			Matcher m = p.matcher(sHtml);
			ArrayList<List<String>> linkList = new ArrayList<List<String>>();
			int k = 0;
			while (m.find()) {
				List<String> rows = new ArrayList<String>();
				for (int i = 0; i <= m.groupCount(); i++) {
					String link = m.group(i);
					link = link == null ? "" : link.trim();
					rows.add(link);
				}
				k++;
				linkList.add(rows);
			}
			return linkList;
		} catch (RuntimeException e) {
			LOG.info("正则匹配出现异常!");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param sHtml
	 * @param mode 正则
	 * @param i 匹配第几个组
	 * @return
	 */
	public static ArrayList<String> retrieveLinks(String sHtml, String mode, int i, Integer pattern) {
		Pattern p = Pattern.compile(mode, pattern);
		try {
			if ((sHtml == null) || (sHtml == "")) return null;
			Matcher m = p.matcher(sHtml);
			ArrayList<String> linkList = new ArrayList<String>();
			while (m.find()) {
				String link = m.group(i).trim();
				linkList.add(link);
			}
			return linkList;
		} catch (RuntimeException e) {
			LOG.info("正则匹配出现异常!");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 用正则获取网上的信息
	 * 
	 * @param str
	 * @param mode "<span>\\s*\\n*
	 *            <hr>
	 *            \\s*\\n*\\t*<br>
	 *            ([^/]+)<br>
	 *            \\s*\\n*\\t*</div>"
	 * @param i 匹配第几个组
	 * @return
	 */
	public static String regStr(String sHtml, String mode, int i) {
		Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
		Matcher m = p.matcher(sHtml);
		String reg_s = null;
		if (m.find()) {
			reg_s = m.group(i);
		}
		return reg_s;
	}

	/**
	 * 用正则获取网上的信息
	 * 
	 * @param str
	 * @param mode "<span>\\s*\\n*
	 *            <hr>
	 *            \\s*\\n*\\t*<br>
	 *            ([^/]+)<br>
	 *            \\s*\\n*\\t*</div>"
	 * @param i 匹配第几个组
	 * @return
	 */
	public static List<String> regStrs(String sHtml, String mode) {
		Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL + Pattern.MULTILINE);
		List<String> list = Lists.newArrayList();
		Matcher m = p.matcher(sHtml);
		if (m.find()) {
			for (int i = 0; i <= m.groupCount(); i++) {
				list.add(m.group(i));
			}
		}
		return list;
	}

	/**
	 * 用正则获取网上的信息
	 * 
	 * @param str
	 * @param mode "<span>\\s*\\n*
	 *            <hr>
	 *            \\s*\\n*\\t*<br>
	 *            ([^/]+)<br>
	 *            \\s*\\n*\\t*</div>"
	 * @param i 匹配几个组
	 * @return
	 */
	public static List<String> regStrBeforeIndex(String sHtml, String mode, int i) {
		Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
		Matcher m = p.matcher(sHtml);
		List<String> reg_s = new ArrayList<String>();
		if (m.find()) {
			for (int j = 0; j <= i; j++) {
				String str = StringUtils.trimToEmpty(m.group(j));
				reg_s.add(str);
			}
		}
		return reg_s;
	}

	/**
	 * 用正则获取网上的信息
	 * 
	 * @param str
	 * @param mode "<span>\\s*\\n*
	 *            <hr>
	 *            \\s*\\n*\\t*<br>
	 *            ([^/]+)<br>
	 *            \\s*\\n*\\t*</div>"
	 * @param i 匹配第几个组
	 * @return
	 */
	public static String regStr(String sHtml, String mode, int i, int pattern) {
		Pattern p = Pattern.compile(mode, pattern);
		Matcher m = p.matcher(sHtml);
		String reg_s = null;
		if (m.find()) {
			reg_s = m.group(i);
		}
		return reg_s;
	}

	/**
	 * 验证字符串是否符合某个正则式
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static boolean regMathcher(String str, String regex) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		return m.find();
	}

	/**
	 * 验证一组表达式里，是否有符合的字符串
	 * 
	 * @param shtml
	 * @param modes
	 * @return
	 */
	public static String regStr(String shtml, List<String> modes) {
		if (modes == null || modes.isEmpty()) return null;

		for (String s : modes) {
			String result = regStr(shtml, s, 0, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
			if (StringUtils.isNotBlank(result)) {
				return result;
			}
		}
		return null;
	}

	public static void main(String[] arg) throws IOException {
		String html = new String(IOUtils.toByteArray(new FileInputStream(new File("E:\\mytemp\\xiechen2.html"))),"utf-8");
		
		String c = RegHtmlUtil.regStr(html, "ajaxRequest\\(\"(http://.*)\"\\);.*</script>.*<input type=\"hidden\" id=\"CorrelationId\"");
		System.out.println(c);
	}
}
